<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends BaseController {





    public function order_process()
    {
      

      $this->display();
    }
   
    public function order()
    {   
      
      $key=I('key');

        if($key&&$key!=""){
           /* $map1 = array(
                'id' =>array('eq', $key),
                'desc'=>array('like', "%".$key."%"),                
                '_logic'     =>'or',
            );*/
            $map['desc']=array('like', "%".$key."%");
            
        }


      $map['user_id']=session('user.id');
      $user_con['id']=session('user.id');
      $user=M('user')->where($user_con)->find();
      //var_dump($map);exit();
      $Nowpage = $_GET['p']?$_GET['p']:1;
      $count      = M('order')->where($map)->count();
      $pageSize=10;
        $Page = getHomepage($count,$pageSize);
      $show       = $Page->show();

        $orders=M('order')->where($map)->page($Nowpage,$pageSize)->order('addtime desc')->select();
       $this->assign('user',$user)->assign('orders',$orders)->assign('orderNum',$count)->assign('page',$show)->display();
    }


      public function gzorder()
    {   
     
      $map['user_id']=session('user.id');
      $userid=session('user.id');

      $Nowpage = $_GET['p']?$_GET['p']:1;
      $count   = M('order_gz')->where($map)->count();
      $pageSize=10;
      $Page = getHomepage($count,$pageSize);
      $show = $Page->show();
      $orders=M('order_gz')->where($map)->page($Nowpage,$pageSize)->order('addtime desc')->select();

       $this->assign('myorder_nav',1)->assign('orders',$orders)->assign('orderNum',$count)->assign('page',$show)->display();
    }



    public function addorder ()
    {   

        if (!IS_POST)
        {
        
           $this->assign('orders',$orders)->display();
        }
        else
        {   
          $jifen=M('jifen')->find();
          $userid= session('user.id');
            $data=$_POST;
            $data['addtime'] = time();
            $data['user_id'] = $userid;
            $data['invite_id'] = M('user')->where("id=$userid")->getField('invite_id');
            $moshi=M('moshi')->where('status=1')->find();
            $jifen=M('jifen')->find();
            $data['order_code'] = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            $data['moshi_week1_num'] = $moshi['moshi_week1_num'];
            $data['moshi_week2_num'] = $moshi['moshi_week2_num'];
            $data['moshi_cf_week1'] = $moshi['moshi_cf_jifen1']*$data['order_num'];
            $data['moshi_xf_week1'] = $moshi['moshi_xf_jifen1']*$data['order_num'];
            $data['moshi_xf_week2'] = $moshi['moshi_xf_jifen2']*$data['order_num'];
            $data['moshi_cf_week2'] = $moshi['moshi_cf_jifen2']*$data['order_num'];
            $data['moshi_total_xf'] = $jifen['jifen_xf_one']*$data['order_num'];
            $data['xf_jifen'] = $jifen['jifen_xf_one'];
            $data['jifen_xf_ten'] = $jifen['jifen_xf_ten'];

            $res=M('order')->add($data);
            if ($res) {
              // $this->success("下单成功,前往支付",U('Order/orderpay',array('orderid'=>$res)));
              $this->redirect('orderpay',array('orderid'=>$res));
            }
        }

    }

  
    //订单支付
      public function orderpay()
    { 
        $orderid=I('orderid');
         $userid= session('user.id');
         $user=M('user')->find($userid);
       $detail = M('order')->find($orderid);
      $this->assign('detail',$detail);
      $this->assign('user',$user);
      $this->display();
    }

      //用财富积分支付

     public function paybycf()
    { 

      $userid=I('userid');
      $order_id=I('order_id');

      $user=M('user')->find($userid);
      $order=M('order')->find($order_id);
    
      if ($user['cf_jifen']>=$order['moshi_total_xf'] ){
        M('user')->where(array('id'=>$userid))->setDec('cf_jifen',$order['moshi_total_xf']);
        $record['user_id']=$userid;
         $record['items']='购买报单';
         $record['jifen_num']='-'.$order['moshi_total_xf'];
         $record['addtime']=time();
         $record['jifen_type']='cf';
         M('jifen_record')->add($record);
 

          $r=$this->edit_after_addorder($order_id);
         if ($r) {
           $this->success('支付成功',1,1);
         }
         
      }else{
       
        $this->success('财富积分不足',1,1);
      }
    
    }

     public function tixian()
    { 
      $tixian_fee=M('sys')->getField('tixian_fee');
      $map['user_id']=session('user.id');
      $lists=M('tixian')->where($map)->select();
      $this->assign('tixian_fee',$tixian_fee);
      $this->assign('jifen',1);
      $this->assign('lists',$lists);
      $this->display();
    }

     public function gz()
    { 

      $userid=session('user.id');
      $user=M('user')->find($userid);
      $jfzr_opne=M('sys')->getField('jfzr_open');
      $this->assign('jifen',1);
      $this->assign('jfzr_opne',$jfzr_opne);
      $this->assign('user',$user);
      $this->display();
    }
    
    public function option( )
    { $cur_time=time();
      $this->assign('option_nav',1);
      $this->assign('cur_time',$cur_time);
      $this->display();
    }

     
        public function edit_after_addorder($orderid)
    {
        $curtime=time();
        $jifen_xf_one= M('jifen')->getField('jifen_xf_one');
        $orderinfo= M('order')->find($orderid);
        $data['audit_time']=$curtime;
        $data['id']=$orderid;
        $data['paystatus']=1;
        
        $res1=M('order')->save($data);
        $userid=M('order')->where("id=$orderid")->getField('user_id');
        $userinfo=M('user')->find($userid);
        $invite_id=$userinfo['invite_id'];
        $f_id=$userinfo['f_id'];
         $add_xfjifen=$orderinfo['moshi_total_xf'];
         $res2=M('user')->where("id=$userid")->setInc("xf_jifen",$add_xfjifen);
         $res3=M('user')->where("id=$userid")->setInc("ordernums",$orderinfo['order_num']);
         M('user')->where("id=$userid")->setInc("self_flordernum",$orderinfo['order_num']);
         $record['user_id']=$userid;
         $record['items']='下单奖励积分';
         $record['jifen_num']='+'.$add_xfjifen;
         $record['addtime']=$curtime;
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
        $status=M('user')->where("id=$userid")->getField('status');

        $moshi=M('moshi')->where('status=1')->find();
          $weeknum=$orderinfo['moshi_week1_num']+$orderinfo['moshi_week2_num'];
                    $a = date("w",$data['audit_time']);
            if($a =="0"){
                  $d=date("Y-m-d 00:00:00",$data['audit_time']);
            }else{
                $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['audit_time']));
            }
            $curtime=time();
           for ($i=1; $i <= $orderinfo['moshi_week1_num']; $i++) { 
                //unset($data);
                $data2['order_id'] =$orderid;
                $data2['orderinfo'] = $orderinfo['orderinfo'];
                $data2['hf_endtime'] =strtotime($d)+604800*$i; 
                $data2['cf_jifen'] =$orderinfo['moshi_cf_week1'];
                $data2['xf_jifen'] =$orderinfo['moshi_xf_week1'];
                if ($data2['hf_endtime']<=$curtime) {
                  $data2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$orderinfo['moshi_cf_week1']);
             $record['user_id']=$userid;
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$orderinfo['moshi_cf_week1'];
             $record['addtime']=$data2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

                M('order')->where("id=".$orderid)->setInc('moshi_week1_fh_num');
                }else{
                  $data2['status']=0;
                }
               
                M('order_fh')->add($data2);
            }

             for ($i=$orderinfo['moshi_week1_num']+1; $i <= $weeknum; $i++) { 
                //unset($data);
                $data3['order_id'] =$orderid;
                $data3['orderinfo'] = $orderinfo['orderinfo'];
                $data3['hf_endtime'] =strtotime($d)+604800*$i;
                $data3['xf_jifen'] =$orderinfo['moshi_xf_week2'];
                $data3['cf_jifen'] =$orderinfo['moshi_cf_week2'];
                if ($data3['hf_endtime']<=$curtime) {
                  unset($record);
                  $data3['status']=1;
                  M('user')->where("id=$userid")->setInc("cf_jifen",$orderinfo['moshi_cf_week2']);
                  M('user')->where("id=$userid")->setInc("xf_jifen",$orderinfo['moshi_xf_week2']);

             $record['user_id']=$userid;
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$orderinfo['moshi_cf_week2'];
             $record['addtime']=$data3['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

             $record2['user_id']=$userid;
             $record2['items']='每周返还消费积分';
             $record2['jifen_num']='+'.$orderinfo['moshi_xf_week2'];
             $record2['addtime']=$data3['hf_endtime'];
             $record2['jifen_type']='xf';
             M('jifen_record')->add($record2);

              M('order')->where("id=".$orderid)->setInc('moshi_week2_fh_num');
                }else{
                  $data3['status']=0;
                }

                M('order_fh')->add($data3);
            }
    

         $map['user_id']=$userid;
      
         $map['paystatus']=1;
          //$orderxf_num=$userinfo['ordernums'];
         $orderxf_num=D('order')->getTotalNums($map);
     

          //自身消费满10单，送上级积分 
          if ($orderxf_num>=10) {
            //总分的工资订单数
         $yfh_gz=floor($orderxf_num/10);
         $c['user_id']=$invite_id;
         $c['from_userid']=$userid;
         $c['items']='您邀请的好友超10单奖励积分';
         $c['jifen_type']='xf';
  
            $fh_jf=M('jifen_record')->where($c)->count();

            $n=$yfh_gz-$fh_jf;

          for ($i=1; $i <=$n ; $i++) { 
         $record['user_id']=$invite_id;
         $record['from_userid']=$userid;
         $record['items']='您邀请的好友超10单奖励积分';
         $record['jifen_num']='+'.$orderinfo['jifen_xf_ten'];
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
          M('user')->where("id=$invite_id")->setInc("xf_jifen",$orderinfo['jifen_xf_ten']); 
          }
        
            $f_id=M('user')->where("id=$userid")->getField('f_id');
       
    $up_userinfo=M('user')->where('id='.$invite_id)->find();
     if ($status==1&&$up_userinfo['status']>2) {

            if ($up_userinfo['f_id']==0) {
                D('order')->xf_total_nums($invite_id,2);
            }else{
                  D('order')->xf_total_nums($invite_id,1);
            }
            
              
    }

 }
 
 $userinfo=M('user')->find($userid);
  if ($userinfo['status']>1) {
                $map['user_id']=$userid;
                 $map['paystatus']=1;
              switch ($userinfo['status']) {
                    case '2':
                         $map['audit_time']=array('GT',$userinfo['sj_time']);
                        break;
                    case '3':
                        $map['audit_time']=array('GT',$userinfo['sj3_time']);
                        break;
                    case '4':
                        $map['audit_time']=array('GT',$userinfo['sj34_time']);
                        break;    
                    default:
                        # code...
                        break;
                }

              $orderxf_fhnum=$userinfo['self_flordernum'];
             if ($orderxf_fhnum>=10) {
                if ($f_id==0) {
                
                D('order')->self10_half_getOrderNums_up($userid,$orderxf_fhnum,'自身消费满10单',2);
                }
                if ($f_id!=0) {
                   
                    D('order')->self10_half_getOrderNums_up($userid,$orderxf_fhnum,'自身消费满10单',1);
                } 
             }
            
              
    }
    
      if ($invite_info['status']>1&&$userinfo['status']==1) {
         M('user')->where('id='.$invite_id)->setInc('self_ztxf',$orderinfo['order_num']);
    }
   $jb=M('jbset')->find();

       switch ($status) {
              case '1':
                  $months=$jb['sj_jb2_mons']-1;
                  break;
            case '2':
                  $months=$jb['sj_jb3_mons']-1;
                  break;
            case '3':
                   $months=$jb['sj_jb4_mons']-1;
                  break;
              
              default:
                  # code...
                  break;
          }

        $end=time();
         $begin=mktime(0,0,0,date('m')-$months,1,date('Y'));
D('Order')->getOrderNums_fl($userid,$orderinfo['order_num']);
 $iteration =D('Order')->getOrderNums_up($userid,$orderinfo['order_num']); 
$getOrderNums_team = D('Order')->getTeamTotalNumsUpCum($userid,$begin,$end);

        //获取上级id 自身团队
        $invite_id=M('user')->where("id=$userid")->getField('invite_id');
        if ($invite_id) {
             //是否达到自动解绑
            $invite_status=M('user')->where("id=$invite_id")->getField('status');
             if ($status>=$invite_status && $status>1 ) {
               
                  $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$invite_id;
                          $beyond['beyond_time']=time();
                      M('user')->where("id=$userid")->save($beyond);
            }

     
     

        }
       
         if ($res2 && $res1 && $res3) {
            M()->commit(); 
           return true;
         }else{
           M()->rollback();
           return false;
         }

    }



   
    



}
